﻿using Engine;
using Engine.Graphics;
using Game.NetWork;
using GameEntitySystem;
using TemplatesDatabase;

namespace Game
{
    public class FCComponentMicro : ComponentInventoryBase, IUpdateable
    {
        public double m_nextupdatetime = 0.0;

        private bool isRun;

        public SubsystemTerrain m_subsystemTerrain;

        public SubsystemModelsRenderer subsystemModelsRenderer;

        public PrimitivesRenderer3D m_primitivesRenderer3D = new PrimitivesRenderer3D();

        public ComponentBlockEntity m_componentBlockEntity;

        public float m_smeltingProgress;

        public int ResultSlotIndex => SlotsCount - 1;

        public int FuelSlotIndex => SlotsCount - 2;

        public float SmeltingProgress => m_smeltingProgress;

        public UpdateOrder UpdateOrder => UpdateOrder.Default;

        public override void Load(ValuesDictionary valuesDictionary, IdToEntityMap idToEntityMap)
        {
            base.Load(valuesDictionary, idToEntityMap);
            m_componentBlockEntity = base.Entity.FindComponent<ComponentBlockEntity>(throwOnError: true);
            subsystemModelsRenderer = base.Project.FindSubsystem<SubsystemModelsRenderer>(throwOnError: true);
        }

        public override int GetSlotCapacity(int slotIndex, int value)
        {
            if (slotIndex == FuelSlotIndex)
            {
                if (value == Terrain.MakeBlockValue(22, 0, 0))
                {
                    return base.GetSlotCapacity(slotIndex, value);
                }

                return 0;
            }

            if (slotIndex == ResultSlotIndex)
            {
                if (value == Terrain.MakeBlockValue(972, 0, 13))
                {
                    return base.GetSlotCapacity(slotIndex, value);
                }

                return 0;
            }

            return base.GetSlotCapacity(slotIndex, value);
        }

        public bool ProcessOver()
        {
            Block[] blocks = BlocksManager.Blocks;
            foreach (Block block in blocks)
            {
                foreach (int creativeValue in block.GetCreativeValues())
                {
                    if (block.GetCategory(creativeValue) == "Plants" || (block.GetCategory(creativeValue) == "Food" && block.GetSicknessProbability(creativeValue) > 0f && m_slots[0].Value == creativeValue))
                    {
                        return true;
                    }
                }
            }

            return false;
        }

        public bool ProcessBegin()
        {
            Block[] blocks = BlocksManager.Blocks;
            foreach (Block block in blocks)
            {
                foreach (int creativeValue in block.GetCreativeValues())
                {
                    if (m_slots[0].Value == Terrain.MakeBlockValue(2, 0, 0) && m_slots[FuelSlotIndex].Count > 0 && m_slots[ResultSlotIndex].Count < 100)
                    {
                        return true;
                    }
                }
            }

            return false;
        }

        public void Update(float dt)
        {
            if (CommonLib.WorkType == WorkType.Client)
            {
                return;
            }

            if (ProcessBegin())
            {
                m_smeltingProgress = MathUtils.Min(m_smeltingProgress + 0.15f * dt, 1f);
                isRun = true;
            }

            if (m_smeltingProgress >= 1f)
            {
                if (ProcessOver())
                {
                    m_slots[ResultSlotIndex].Value = Terrain.MakeBlockValue(972, 0, 13);
                    m_slots[ResultSlotIndex].Count++;
                    OnSlotChange(ResultSlotIndex);
                }

                m_smeltingProgress = 0f;
                m_slots[0].Count--;
                m_slots[FuelSlotIndex].Count--;
                OnSlotChange(FuelSlotIndex);
                OnSlotChange(0);
            }

            if (m_slots[0].Count == 0 || m_slots[FuelSlotIndex].Count == 0)
            {
                m_smeltingProgress = 0f;
            }

            if (isRun && CommonLib.WorkType != WorkType.Client && m_smeltingProgress == 0f)
            {
                isRun = false;
                CommonLib.Net.QueuePackage(new ComponentMicroPackage(base.Entity.EntityId, m_smeltingProgress));
            }
        }
    }
}